//
//  LYRadioButtonGroupView.swift
//  LYOnlineBanking
//
//  Created by 李永彬 on 2025/7/26.
//

import UIKit
import SnapKit

protocol LYRadioButtonGroupViewDelegate: AnyObject {
    func radioButtonGroupView(_ view: LYRadioButtonGroupView, didSelectButtonAt index: Int)
}

class LYRadioButtonGroupView: UIView {
    
    // MARK: - Properties
    
    weak var delegate: LYRadioButtonGroupViewDelegate?
    
    private var buttons: [UIButton] = []
    private var selectedIndex: Int = 0 {
        didSet {
            updateButtonStates()
            delegate?.radioButtonGroupView(self, didSelectButtonAt: selectedIndex)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - Setup
    
    func setDatas(titles: [String], normalImages: [String], selectedImages: [String]) {
        setupButtons(titles: titles, normalImages: normalImages, selectedImages: selectedImages)
        setupLayout()
    }
    
    private func setupButtons(titles: [String], normalImages: [String], selectedImages: [String]) {
        for (index, title) in titles.enumerated() {
            let button = UIButton(type: .custom)
            button.tag = index
            button.setTitle(title, for: .normal)
            button.setTitle(title, for: .selected)
            button.setTitleColor(UIColor(hex: "#FFFFFF"), for: .normal)
            button.setTitleColor(UIColor(hex: "#FFFFFF"), for: .selected)
            button.titleLabel?.font = UIFont.systemFont(ofSize: 11 * xScale)
            button.layer.cornerRadius = 5
            
            if index < normalImages.count {
                let imageName = normalImages[index]
                button.setBackgroundImage(UIImage(named: imageName), for: .normal)
            }
            
            if index < selectedImages.count {
                let selectedImageName = selectedImages[index]
                button.setBackgroundImage(UIImage(named: selectedImageName), for: .selected)
            }
            
            button.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside)
            buttons.append(button)
            addSubview(button)
        }
        
        // 默认选中第一个
        if !buttons.isEmpty {
            buttons[0].isSelected = true
        }
    }
    
    private func setupLayout() {
        guard !buttons.isEmpty else { return }
        
        let buttonCount = buttons.count

        for (index, button) in buttons.enumerated() {
            button.snp.makeConstraints { make in
                make.top.bottom.equalToSuperview()
                
                if index == 0 {
                    make.leading.equalToSuperview()
                } else {
                    make.leading.equalTo(buttons[index - 1].snp.trailing).offset(0)
                    make.width.equalTo(buttons[index - 1])
                }
                
                if index == buttonCount - 1 {
                    make.trailing.equalToSuperview()
                }
            }
        }
    }
    
    // MARK: - Actions
    @objc private func buttonTapped(_ sender: UIButton) {
        selectedIndex = sender.tag
    }
    
    private func updateButtonStates() {
        for (index, button) in buttons.enumerated() {
            button.isSelected = (index == selectedIndex)
            if button.isSelected {
                button.alpha = 1.0
            } else {
                button.alpha = 0.5
            }
        }
    }
    
    // MARK: - Public Methods
    
    func selectButton(at index: Int) {
        guard index >= 0 && index < buttons.count else { return }
        selectedIndex = index
        updateButtonStates()
    }
    
    func getSelectedIndex() -> Int {
        return selectedIndex
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        setupLayout()
    }
}
